package com.example.cloud.controller;

import com.example.cloud.entities.PayDTO;
import com.example.cloud.resp.ResultData;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2024-03-06
 * Time: 21:28
 */

@RestController
@RequestMapping("/consumer")
public class OrderController {

    //    public static final String PaymentSrv_URL = "http://localhost:8001"; // 通过端口直接访问，不能负载均衡
    public static final String PaymentSrv_URL = "http://cloud-payment-service"; // 通过注册的服务名字访问


    @Resource
    private RestTemplate restTemplate;


    @GetMapping("/pay/add")
    public ResultData createOrder(PayDTO payDTO) {
        return restTemplate.postForObject(PaymentSrv_URL + "/pay/add", payDTO, ResultData.class);
    }


    @DeleteMapping("/del/{id}")
    public ResultData delete(@PathVariable("id") int id) {
        // 修改为 delete 请求，因为 8001 端口式 deleteMapping 接受的
        return restTemplate.exchange(PaymentSrv_URL + "/pay/del/" + id, HttpMethod.DELETE, HttpEntity.EMPTY, ResultData.class).getBody();
    }

    @GetMapping("/pay/update")
    public ResultData updatePay(PayDTO payDTO) {
        // 修改为 put 请求，因为 8001 端口式 putMapping 接受的
        return restTemplate.exchange(PaymentSrv_URL + "/pay/update", HttpMethod.PUT, new HttpEntity<>(payDTO), ResultData.class).getBody();
    }

    @GetMapping("/pay/get/{id}")
    public ResultData getPayInfo(@PathVariable("id") int id) {
        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/" + id, ResultData.class, id);
    }


    @GetMapping("/pay/getAll")
    public ResultData getAll() {
        return restTemplate.getForObject(PaymentSrv_URL + "/pay/getAll", ResultData.class);
    }


    @GetMapping("/pay/get/info")
    public String getInfoByConsul() {
        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/info", String.class);
    }


    // 负载均衡的底层原理
    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("/discovery")
    public String discovery() {
        // 1. 从注册中心，获取所有的可用微服务的名称
        List<String> services = discoveryClient.getServices();
        for (String element : services) {
            System.out.println(element);
        }

        System.out.println("===================================");
        // 2. 获取指定微服务的实例信息，里面存放着该服务名下的所有端口服务
        List<ServiceInstance> instances = discoveryClient.getInstances("cloud-payment-service");
        for (ServiceInstance element : instances) {
            System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t" + element.getUri());
        }

        // 3. 自定义实现算法，返回相应的端口服务
        return instances.get(0).getServiceId() + ":" + instances.get(0).getPort();
    }


}
